﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;

public partial class User_dang_ky : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserEmail"] == null & Request.Cookies["UserEmail"]==null)
            Response.Redirect("~/User/dang-nhap.aspx");
        else if (Session["UserEmail"] != null & Request.Cookies["UserEmail"] == null)
        {
            if (!IsPostBack)
            {
                SQLHelper Dal = new SQLHelper();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_GetUserAvatar";
                Dal.ParameterCollection = new string[1] { "@UserEmail" };
                Dal.ValueCollection = new string[1] { Session["UserEmail"].ToString() };
                DataRow dr = Dal.GetDataRow();
                if (String.IsNullOrEmpty(dr[0].ToString()))
                    avatar.ImageUrl = ConfigurationManager.AppSettings["DefaultAvatar"].ToString();
                else
                avatar.ImageUrl = ConfigurationManager.AppSettings["UserAvatar"] + dr[0].ToString();

            }
        }
        else if (Session["UserEmail"] != null & Request.Cookies["UserEmail"] != null)
        {
            if (!IsPostBack)
            {
                SQLHelper Dal = new SQLHelper();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_GetUserAvatar";
                Dal.ParameterCollection = new string[1] { "@UserEmail" };
                Dal.ValueCollection = new string[1] { Session["UserEmail"].ToString() };
                DataRow dr = Dal.GetDataRow();
                if (String.IsNullOrEmpty(dr[0].ToString()))
                    avatar.ImageUrl = ConfigurationManager.AppSettings["DefaultAvatar"].ToString();
                else
                    avatar.ImageUrl = ConfigurationManager.AppSettings["UserAvatar"] + dr[0].ToString();

            }
        }
        else if (Session["UserEmail"] == null & Request.Cookies["UserEmail"] != null)
        {
            if (Request.Cookies["UserEmail"].Value != "")
            {
                if (!IsPostBack)
                {
                    SQLHelper Dal = new SQLHelper();
                    Dal.CommandType = CommandType.StoredProcedure;
                    Dal.CommandText = "SP_GetUserAvatar";
                    Dal.ParameterCollection = new string[1] { "@UserEmail" };
                    Dal.ValueCollection = new string[1] { Request.Cookies["UserEmail"].ToString() };
                    DataRow dr = Dal.GetDataRow();
                    if (String.IsNullOrEmpty(dr[0].ToString()))
                        avatar.ImageUrl = ConfigurationManager.AppSettings["DefaultAvatar"].ToString();
                    else
                        avatar.ImageUrl = ConfigurationManager.AppSettings["UserAvatar"] + dr[0].ToString();

                }
            }
            else
            {
                Response.Redirect("~/User/dang-nhap.aspx");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!cbResetAvatar.Checked)
        {
            if (fuAvatar.HasFile)
            {
                //try
                //{
                    string filename = Path.GetFileName(fuAvatar.FileName);
                    fuAvatar.SaveAs(Server.MapPath("~/Images/UserAvatar/" + filename));
                    SQLHelper Dal = new SQLHelper();
                    Dal.Open();
                    Dal.CommandType = CommandType.StoredProcedure;
                    Dal.CommandText = "SP_UpdateUsersAvatar";
                    Dal.ParameterCollection = new string[2] { "@UserEmail", "@HinhDaiDien" };
                    Dal.ValueCollection = new string[2] { Session["UserEmail"].ToString(),filename };
                    Dal.ExecuteNonQuery();
                    Dal.Close();
                    Dal.Dispose();
                    Reloaddata();
                    lblInfo.Text = "Cập nhật thành công";
                //}
                //catch
                //{
                //    lblInfo.Text = "Lỗi hệ thống, vui lòng cập nhật lại";
                //}
            }
        }
        else
        {
            try
            {
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_ResetAvatar";
                Dal.ParameterCollection = new string[1] { "@UserEmail" };
                Dal.ValueCollection = new string[1] { Session["UserEmail"].ToString() };
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
                Reloaddata();
                lblInfo.Text = "Bạn đã sử dụng hình đại diện mặc định";
                
            }
            catch
            {
                lblInfo.Text = "Lỗi hệ thống,reset hình đại diện không thành công, vui lòng thử lại";
            }
        }
    }
    protected void Reloaddata()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetUserAvatar";
        Dal.ParameterCollection = new string[1] { "@UserEmail" };
        Dal.ValueCollection = new string[1] { Session["UserEmail"].ToString() };
        DataRow dr = Dal.GetDataRow();
        if (String.IsNullOrEmpty(dr[0].ToString()))
            avatar.ImageUrl = ConfigurationManager.AppSettings["DefaultAvatar"].ToString();
        else
            avatar.ImageUrl = ConfigurationManager.AppSettings["UserAvatar"] + dr[0].ToString();
        Image img = (Image)Master.FindControl("Image5");
        if (String.IsNullOrEmpty(dr[0].ToString()))
            img.ImageUrl = ConfigurationManager.AppSettings["DefaultAvatar"].ToString();
        else
            img.ImageUrl = ConfigurationManager.AppSettings["UserAvatar"] + dr[0].ToString();
    }
}
